<template>
	<!-- 导航栏 -->
	<el-breadcrumb separator="/">
		<el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
		<el-breadcrumb-item>内容管理</el-breadcrumb-item>
		<el-breadcrumb-item>文章分类管理</el-breadcrumb-item>
	</el-breadcrumb>

	<!-- 行内表单 -->
	<el-form :inline="true" :model="queryInfo" class="demo-form-inline" style="padding: 15px 0">
		<el-form-item label="文章名">
			<el-input v-model="queryInfo.sortName" placeholder="请输入文章分类名" clearable @clear="queryDataList" />
		</el-form-item>

		<el-form-item>
			<el-button type="primary" @click="queryDataList">搜索</el-button>
			<el-button type="success" @click="add">新增分类</el-button>
			<el-button type="danger" @click="removeBatch">批量删除</el-button>
		</el-form-item>
	</el-form>


	<!-- 表格数据 -->
	<el-table :data="dataList" border row-key="id" style="width: 100%" @selection-change="handleSelectionChange">
		<el-table-column fixed="left" type="selection" width="55" />
		<el-table-column fixed="left" property="id" label="ID" width="60" />
		<el-table-column property="sortName" label="分类名" />
		<el-table-column property="del" label="状态">
			<template #default="scope">
				<el-tag type="success" v-if="scope.row.del==0">正常</el-tag>
				<el-tag type="danger" v-else>已删除</el-tag>
			</template>
		</el-table-column>
		<el-table-column fixed="right" label="操作" min-width="120">
			<template #default="scope">
				<el-button type="success" size="small" @click="edit(scope.row)">编辑</el-button>
				<el-button type="danger" size="small" @click="remove(scope.row.id)">删除</el-button>
			</template>
		</el-table-column>
	</el-table>

	<!-- 分页区域 -->
	<el-pagination v-model:current-page="queryInfo.pageNum" v-model:page-size="queryInfo.pageSize"
		:page-sizes="[5, 10, 15, 20]" default-page-size="5" size="default" layout="total, sizes, prev, pager, next, jumper" :total="total"
		@size-change="handleSizeChange" @current-change="handleCurrentChange" />

	<!-- Dialog对话框 -->
	<el-dialog :title="title" v-model="dialogFormVisible" width="950" @close="cancle">
		<el-form :model="classificationDate" label-width="100">
			<el-form-item label="分类名">
				<el-input v-model="classificationDate.sortName" autocomplete="off"
					placeholder="请输入分类名"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<div class="dialog-footer">
				<el-button @click="cancle">取 消</el-button>
				<el-button type="primary" @click="confirm">
					确 定
				</el-button>
			</div>
		</template>
	</el-dialog>

</template>

<script setup lang="ts">
	import { ElMessage, ElMessageBox } from "element-plus";
	import { getCurrentInstance, onMounted, reactive, ref } from "vue";
	import { cloneDeep } from 'lodash-es';

	// 查询对象信息
	const queryInfo = reactive({
		id: '',
		pageNum: 1,
		pageSize: 5,
		sortName: ''
	})

	const total = ref()

	const dataList = ref([])

	const axios = getCurrentInstance().appContext.config.globalProperties.axios;

	// 定义查询数据的函数
	const queryDataList = () => {
		axios.get('articleSort/page', {
			params: queryInfo
		}).then((resp) => {
			if (resp.code == 200) {
				dataList.value = resp.data.list;
				total.value = resp.data.total;
			} else {
				ElMessage.error(resp.message)
			}
		})
	}

	// 组件加载完毕，加载数据
	onMounted(() => {
		queryDataList()
	})

	// 存储复选的数据
	const multipleSelection = ref([])

	const handleSelectionChange = (val) => {
		multipleSelection.value = val.map((item) =>{
			return item.id
		})
	}

	/* 每页显示条目数改变 */
	const handleSizeChange = (val : number) => {
		queryDataList()
	}
	/* 页码改变 */
	const handleCurrentChange = (val : number) => {
		queryDataList()
	}

	const title = ref('新增分类')

	const dialogFormVisible = ref(false)

	const classificationDate = ref({
		sortName: "",
		del: 0,
		id: ""
	})
	
	const cancle = () => {
		console.log('你点击了取消按钮');
		classificationDate.value={
			sortName: "",
			del: 0,
			id: ""
		}
		dialogFormVisible.value=false
	}
	const confirm = () => {
		if(classificationDate.value.id){
			axios.put('articleSort/edit',classificationDate.value).then((resp)=>{
				if(resp.code==200){
					ElMessage.success(resp.message)
				}else{
					ElMessage.error(resp.message)
				}
				queryDataList()
				dialogFormVisible.value=false
			})
		}else{
			console.log('你点击了确定按钮');
			axios.post('articleSort/add',classificationDate.value)
			.then((resp)=>{
				if(resp.code==200){
					ElMessage.success(resp.message)
				}else{
					ElMessage.error(resp.message)
				}
				queryDataList()
				dialogFormVisible.value=false
			})
		}
	}
	
	const add =()=>{
		title.value='新增文章分类'
		dialogFormVisible.value=true
	}
	
	const edit=(row)=>{
		title.value='编辑文章分类'
		dialogFormVisible.value=true
		classificationDate.value=cloneDeep(row)
	}
	
	const remove=(id)=>{
		ElMessageBox.confirm(
			'此操作将永久删除文件，是否继续?',
			'提示',
			{
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
				center: true,
			}).then(() => {
				axios.delete(`articleSort/${id}`)
					.then((resp) => {
						if (resp.code == 200) {
							ElMessage.success(resp.message)
						} else {
							ElMessage.error(resp.message)
						}
						queryDataList()
					})
			}).catch(() => {
				ElMessage({
					type: 'info',
					message: '取消删除',
				})
			})
	}
	
	const removeBatch=()=>{
		ElMessageBox.confirm(
			'此操作将永久删除文件，是否继续?',
			'提示',
			{
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
				center: true,
			}).then(() => {
				axios.delete(`articleSort?ids=${multipleSelection.value.join(',')}`)
					.then((resp) => {
						if (resp.code == 200) {
							ElMessage.success(resp.message)
						} else {
							ElMessage.error(resp.message)
						}
						queryDataList()
					})
			}).catch(() => {
				ElMessage({
					type: 'info',
					message: '取消删除',
				})
			})
	}
</script>

<style scoped>
	.el-breadcrumb {
		/* 下划线 */
		border-bottom: 1px solid #eeeeee;
		padding-bottom: 15px;
	}

	.demo-form-inline .el-input {
		--el-input-width: 220px;
	}

	.demo-form-inline .el-select {
		--el-select-width: 220px;
	}

	.el-pagination {
		margin-top: 15px;
	}
</style>